儲存結構
遍歷
按照某種次序一次訪問一次二元樹中的所有節點
前序遍歷
程式碼
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
//以前序方式輸入數據
CreateBiTree(BiTree *t)
{
char c;
scanf("%c",&c);
if(' ' == c)
{
*t = NULL;
}else
{
*t = (BiTNode *)malloc(sizeof(BiTNode));
(*t)->data = c;
createBiTree(&(*t)->lchild);
createBiTree(&(*t)->rchild);
}
}
visit(char c, int level)
{
printf("%c in %d\n",c,level);
}
//前序遍歷
TravelBiTree(BiTree T, int level)
{
if( T )
{
visit(T->data, level)
TravelBiTree(T->lchild, level+1);
TravelBiTree(T->rchild, level+1);
}
}